package com.cuitrip.util.text;

import android.text.Editable;
import android.text.TextUtils;
import android.widget.EditText;

import java.util.Calendar;

/**
 * Created by MiJack on 2015/11/29.
 */
public class CardExpiryTextWatcher extends BaseTextWatcher {
    int preYear = -1;
    int preMouth = -1;
    int index = -1;
    private CharSequence sequence;
    private EditText editText;

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        super.beforeTextChanged(s, start, count, after);
        if (isFormatting) {
            return;
        }
        //获取格式化前的year和mouth
        sequence = new String(s.toString());
        index = s.toString().indexOf("/");
        if (index > 0) {
            preMouth = parseInt(s.toString().subSequence(0, index).toString());
            preYear = parseInt(s.toString().subSequence(index + 1, s.length()).toString());
        } else {
            preMouth = parseInt(s.toString());
            preYear = -1;
        }
    }

    private int parseInt(String string) {
        if (TextUtils.isEmpty(string) || "/".equals(string)) {
            return -1;
        }
        return Integer.parseInt(string);
    }

    @Override
    public void format(Editable s) {
        int year;
        int mouth;
        formatDate(s);
        int i = s.toString().indexOf("/");

        if (i > 0) {
            mouth = parseInt(s.toString().subSequence(0, i).toString());
            year = parseInt(s.toString().subSequence(i + 1, s.length()).toString());
        } else {
            mouth = parseInt(s.toString());
            year = -1;
        }
        //合法性检查
        int y = Calendar.getInstance().get(Calendar.YEAR) % 100;
        boolean useBefore;
        if (year < y && year > 9) {
            useBefore = true;
        } else if (year == y) {
            int m = Calendar.getInstance().get(Calendar.MONTH) + 1;
            useBefore = m < mouth;
        } else {
            useBefore = false;
        }
        s.clear();
        if (useBefore) {
            s.append(sequence);
        } else {
            format(s, year, mouth);
        }
    }

    private void format(Editable s, int y, int m) {
        if (m > 12) {
            m = preMouth;
        }
        if (m < 0) {
            return;
        }
        if ((m < 10 && m > 1)//2~9月自动加0
                || (m == 0)//用户输入0
                || (m == 1 && sequence.length() > 0 && sequence.charAt(0) == '0')  //输入01
                ) {
            s.append("0");
        } else {
            s.append("");
        }
        if (m != 0) {
            //防止出现00的情况
            s.append(m + "");
        }
        if (y < 0) {
            return;
        }
        s.append("/");
        //判断是否超过当年的月
        int yn = Calendar.getInstance().get(Calendar.YEAR) % 100;
        int mn = Calendar.getInstance().get(Calendar.MONTH) + 1;
        if (y == yn && mn > m) {
            //使用之前的年
            s.append(preYear + "");
        } else {
            s.append(y + "");
        }
    }

    public void formatDate(Editable text) {
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (c == '.' || c == ' ' || c == '/' || c == '-') {
                text.delete(i, i + 1);
                i--;
            }
        }
        //26-6 ->236-6
        int in = text.toString().indexOf("/");
        if (in > 1 && in != 2) {
            text.delete(in, in + 1);
        } else {
            if (index != -1 && text.length() > index && '/' == text.charAt(index)) {
                text.delete(index - 1, index);
            }
        }
        int index = -1;
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (Character.isDigit(c)) {
                index++;
                //判断是否需要添加-
                if (index % 2 == 0 && index != 0) {
                    text.insert(i, "/");
                    i++;
                }
            } else {
                //移出空格
                text.delete(i, i + 1);
                i--;
            }
        }
    }
}
